﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialShare.DAL.Facades;
using SocialShare.BusinessEntities;
using Utilities;

namespace SocialShare.Web.Admin
{
    public partial class approve_blog_posts : System.Web.UI.Page
    {
        private Int32 PostId
        {
            get
            {
                try
                {
                    if (!string.IsNullOrEmpty(Request.QueryString["postId"].ToString()))
                    {
                        return Convert.ToInt32(Request.QueryString["postId"].ToString());
                    }
                    else
                    {
                        return -1;
                    }
                }
                catch
                {
                    return -1;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            BlogFacade bll = new BlogFacade();
            BlogPost post = bll.GetBlogPostById(PostId);
            litUserID.Text = post.UserId;
            litCreateDate.Text = post.DateCreated.Value.ToShortDateString();
            String postStatus=(post.PostStatus == 5 ? "UNPUBLISH" : " PUBLISH");
            String cssClass = (post.PostStatus == 5 ? "button small white" : "button small blue");
            btnApprove.Text = postStatus;
            btnApprove.CommandArgument = PostId.ToString();
            btnApprove.CommandName = postStatus;
            btnApprove.CssClass = cssClass;

            btnDelete.CommandArgument = PostId.ToString();

            litContent.Text = post.PostContent;

        }
       

       
        protected void btnDelete_Command(object sender, CommandEventArgs e)
        {
            BlogFacade bll = new BlogFacade();
            SocialShare_Blogs post = bll.GetBlogById(PostId);
            post.PostStatus = (Int32)ForumStatus.Deleted;
            bll.UpdateBlog(post);
            LoadData();
        }

        protected void btnApprove_Command(object sender, CommandEventArgs e)
        {
            Int32 postId = Convert.ToInt32(e.CommandArgument.ToString());
            BlogFacade bll = new BlogFacade();
            if (e.CommandName == "UNPUBLISH")
            {
                SocialShare_Blogs post = bll.GetBlogById(postId);
                post.PostStatus = (Int32)BlogStatus.UnPublished;
                bll.UpdateBlog(post);
                LoadData();
            }
            else if (e.CommandName == "PUBLISH")
            {
                SocialShare_Blogs post = bll.GetBlogById(postId);
                post.PostStatus = (Int32)BlogStatus.Published;
                bll.UpdateBlog(post);
                LoadData();
            }
        }
    }
}